using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace gears
{
    class Options
    {
        Texture2D exitTexture;
        Texture2D soundOn, soundOff, brightness;
        Texture2D soundMin, soundPlus, brightnessMin, brightnessPlus;
        int soundValue;
        SpriteFont myFont;

        Vector2 soundPos, soundMinPos, soundPlusPos, brightnessPos, brightnessMinPos, brightnessPlusPos;

        Vector2 exitPosition;

        Game1.State innerState = Game1.State.Options;

        public Options()
        {
            Game1.alpha = 0f;

            soundValue = (int)MediaPlayer.Volume * 10;

            soundPos = new Vector2(270, Game1.screenWidth / 2 - 240);
            soundMinPos = soundPos + new Vector2(30, 70);
            soundPlusPos = soundMinPos + new Vector2(130, 0);
            brightnessPos = new Vector2(270, Game1.screenWidth / 2-90);
            brightnessMinPos = brightnessPos + new Vector2(25, 70);
            brightnessPlusPos = brightnessMinPos + new Vector2(145, 0);



            exitPosition = new Vector2(300, Game1.screenWidth / 2+60);
        }


        public Game1.State InnerState
        {
            set { innerState = value; }
        }

        public Texture2D ExitTexture
        {
            set { exitTexture = value; }
        }
        public Texture2D SoundOn
        {
            set { soundOn = value; }
        }
        public Texture2D SoundOff
        {
            set { soundOff = value; }
        }
        public Texture2D SoundMin
        {
            set { soundMin = value; }
        }
        public Texture2D SoundPlus
        {
            set { soundPlus = value; }
        }
        public Texture2D Brightness
        {
            set { brightness = value; }
        }
        public Texture2D BrightnessMin
        {
            set { brightnessMin = value; }
        }
        public Texture2D BrightnessPlus
        {
            set { brightnessPlus = value; }
        }

        public SpriteFont MyFont
        {
            set { myFont = value; }
        }

        public void Update(GameTime gameTime)
        {
            //Transition Effect~~~~~~~~~~~~~~~~~~~~~~~~~~
            if (Game1.transition)
            {
                Game1.alpha -= 0.05f;

                if (Game1.alpha <= Game1.globalAlpha)
                {
                    Game1.alpha = Game1.globalAlpha;
                    Game1.transition = false;
                    this.innerState = Game1.State.Options;
                    //myMenu.InnerState = State.Menu;
                }
            }
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            //Transition Effect~~~~~~~~~~~~~~~~~~~~~~~~~~~
            if (innerState != Game1.State.Options && Game1.transition == false)
            {
                
                Game1.alpha += 0.03f;

                if (Game1.alpha > 1f)
                {
                    Game1.alpha = 1f;
                    Game1.transition = true;
                    
                    Game1.gameState = innerState;
                }
            }
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            //~~~Set the regions for the input~~~~~~~~~~
            Rectangle exitRect = new Rectangle((int)exitPosition.X, (int)exitPosition.Y, exitTexture.Width, exitTexture.Height);
            Rectangle soundRect = new Rectangle((int)soundPos.X, (int)soundPos.Y, soundOn.Width, soundOn.Height);
            Rectangle minRect = new Rectangle((int)soundMinPos.X, (int)soundMinPos.Y, soundMin.Width, soundMin.Height);
            Rectangle plusRect = new Rectangle((int)soundPlusPos.X, (int)soundPlusPos.Y, soundPlus.Width, soundPlus.Height);
            Rectangle brightMinRect = new Rectangle((int)brightnessMinPos.X, (int)brightnessMinPos.Y, brightnessMin.Width, brightnessMin.Height);
            Rectangle brightPlusRect = new Rectangle((int)brightnessPlusPos.X, (int)brightnessPlusPos.Y, brightnessPlus.Width, brightnessPlus.Height);

            //Check if a region is selected and act accordingly~~~~~~~~~~~
            TouchCollection touchLocations = TouchPanel.GetState();
            foreach (TouchLocation touchLocation in touchLocations)
            {
                if ((touchLocation.State == TouchLocationState.Pressed))
                {
                    Point touchPoint = new Point((int)touchLocation.Position.X, (int)touchLocation.Position.Y);

                    if (exitRect.Contains(touchPoint))
                    {
                        Game1.globalAlpha = Game1.alpha;
                        Game1.mySoundEffects.Play("menuSound");
                        innerState = Game1.State.Menu;
                    }
                    if (soundRect.Contains(touchPoint))
                    {
                        if (soundValue > 0)
                            soundValue = 0;
                        else
                            soundValue = 10;
                    }
                    if (minRect.Contains(touchPoint))
                    {
                        if (soundValue > 0)
                            soundValue--;
                    }
                    if (plusRect.Contains(touchPoint))
                    {
                        if (soundValue < 10)
                            soundValue++;
                    }
                    if (brightMinRect.Contains(touchPoint))
                    {
                        if (Game1.alpha < 0.8f)
                            Game1.alpha += 0.100f;
                    }
                    if (brightPlusRect.Contains(touchPoint))
                    {
                        if (Game1.alpha > 0f)
                            Game1.alpha -= 0.100f;
                    }
                }
            }

            //Check if Back button is pressed
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                Game1.globalAlpha = Game1.alpha;

                innerState = Game1.State.Menu;
            }

            //~~~~Update the sound~~~~~~~~~~~~
            MediaPlayer.Volume = (float)soundValue / 10;


        }


        public void Draw(SpriteBatch spriteBatch)
        {
            //Draw the sound buttons~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            if (soundValue > 0)
            {
                spriteBatch.Draw(soundOn, soundPos, Color.White);
            }
            if (soundValue == 0)
                spriteBatch.Draw(soundOff, soundPos, Color.White);

            spriteBatch.Draw(soundMin, soundMinPos, Color.White);
            spriteBatch.Draw(soundPlus, soundPlusPos, Color.White);

            //Draw the brightness buttons~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            spriteBatch.Draw(brightness, brightnessPos, Color.White);
            spriteBatch.Draw(brightnessMin, brightnessMinPos, Color.White);
            spriteBatch.Draw(brightnessPlus, brightnessPlusPos, Color.White);


            spriteBatch.DrawString(myFont, soundValue.ToString("00"), new Vector2(soundMinPos.X + 80, soundMinPos.Y), Color.Yellow);

            float x = 1 - Game1.alpha; 
            spriteBatch.DrawString(myFont, MathHelper.Clamp(x, 0f, 1f).ToString("0.0"), new Vector2(brightnessMinPos.X + 85, brightnessMinPos.Y), Color.Yellow);

            spriteBatch.Draw(exitTexture, exitPosition, Color.White);
            spriteBatch.Draw(Game1.backgroundTexture, new Rectangle(0, 0, Game1.screenHeight, Game1.screenWidth), Color.White * Game1.alpha);
        }
    }
}
